<?php
uses('L10n');
class AppController extends Controller {
    public $components     = array('Cookie');
    public $helpers        = array('Html', 'Form', 'Javascript');
    public $javascripts    = array('protoculous', 'common');
    public $allowedActions = 'all'; // Provides access for actions which most often do not require any access control.
    public $loginRedirect  = '/users/home'; // where to go after a successful login?
    public $loggedUser     = null; // username of logged in user.
    public $userLocationId = null;
    public $userLocation   = null; // which location user has selected
	public $ext            = '.html';
    public $view           = 'Theme';
    public $theme          = '';
    public $scaffold;

    public function beforeFilter () {
        $this->loadSettings();
        //$this->Cookie->path = $this->base;

        if ($theme = Configure::read('site.theme')) { // website template use
            $this->theme = $theme;
        }
        if ($lang = $this->Cookie->read('User.language')) { // user language
            $this->L10n = new L10n();
            $this->L10n->get('zh');
        }
        if (!Configure::read('site.adsenseId')) { // can't leave adSense id empty
            Configure::write('site.adsenseId', 'pub-8854406757815559');
        }
        if ($location = $this->Cookie->read('User.location')) { // user location
            $this->userLocationId = $this->Cookie->read('User.locationId');
            $this->userLocation   = $location;
        } else if ($this->params['controller'] == 'adCategories' && $this->params['action'] == 'index') {
            $this->redirect('/locations/all');
        } else {
            /*$locationId = Configure::read('site.locationId');

            App::import('Model', 'Location');
            $o_location = new Location();
            $location   = $o_location->field('name', array('id'=>$locationId));

            $this->userLocationId = $locationId;
            $this->userLocation   = $location;

            $this->Cookie->write('User.locationId', $locationId, false, '1 year');
            $this->Cookie->write('User.location', $location, false, '1 year');*/
        }

        $role = $this->Session->read('Auth.User.role');
        if (substr($this->params['action'], 0, 6) == 'admin_') { // is in admin panel?
            $this->layout      = 'admin';
            $this->javascripts = am($this->javascripts, 'transmenu');

            if ($this->params['action'] == 'admin_index') {
                $this->set('off_sidebar', 1);
            }
            if (!$role) { // not logged yet
                $this->redirect('/users/home');
            } else if ($role != 'admin') {
                $this->set('pageTitle', __('Invalid access!',true));
                $this->flash('<h4>'.__('Invalid access!',true).'</h4>', '/', 5);
            }
        }
        if ($role == 'admin') { // admin role either in frontend or backend
            $this->javascripts = am($this->javascripts, 'custom_image', 'webtoolkit.aim');
        }

        $this->set('user_role', $role);
    } // beforeFilter

    public function beforeRender () {
        if ($email = $this->Session->read('Auth.User.email')) { // user logged
            $myname = $this->Session->read('Auth.User.nickname');
            $myname = ($myname) ? $myname : $email;
            $this->set('myname', $myname);
        }

        $jscripts = is_array($this->javascripts) ? $this->javascripts : array($this->javascripts);
        $this->set('javascripts', $jscripts);
        $this->set('sitename', Configure::read('site.sitename'));
        $this->set('email', Configure::read('site.email'));
        $this->set('adsenseId', Configure::read('site.adsenseId'));
        $this->set('currentCity', $this->userLocation);
    } // beforeRender

    public function checkAuth () {
        if (isset($this->Auth)) {
            $this->Auth->autoRedirect = false;
            $this->Auth->mapActions(array('display'=>'read'));

            // tell Auth which actions do not need any access control
            if (isset($this->allowedActions)) { // shared between all application's controllers
               $this->Auth->allow($this->allowedActions);
            }
            if (isset($this->publicActions)) { // use in controller extended AppController
               $this->Auth->allow($this->publicActions);
            }

            $this->Auth->fields        = array('username'=>'email', 'password'=>'password');
            $this->Auth->userScope     = array('User.active'=>1);
            $this->Auth->loginAction   = '/users/login';
            $this->Auth->loginRedirect = $this->loginRedirect;
            //$this->Auth->authorize = 'controller';
            $this->Auth->loginError    = '<h4>'.__('Sorry, login failed. Either your username or password are incorrect.', true).'</h4>';
            $this->Auth->authError     = '<h4>'.__('The page you tried to access is restricted. You have been redirected to the page below.', true).'</h4>';
        }
    } // checkAuth

    public function loadSettings () {
        App::import('Model', 'Setting');
        $o_setting = new Setting();

        $params = array(
            'fields' => 'Setting.category, Setting.short_name, Setting.value',
            'order'  => 'Setting.category ASC, Setting.position ASC'
        );
        $settings = $o_setting->find('all', $params);

        foreach ($settings as $data) {
            $key   = $data['Setting']['category'].'.'.$data['Setting']['short_name'];
            $value = $data['Setting']['value'];
            Configure::write($key, $value);
        }
    } // loadSettings
} // AppController
